package org.sky.service.serviceImpl;

import org.sky.base.DaoSupport;
import org.sky.model.PayForOrder;
import org.sky.model.Room;
import org.sky.model.RoomUsed;
import org.sky.service.RoomService;
import org.sky.util.ChangeTime;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-10-21
 * Time: 下午8:34
 * To change this template use FileUtil | Settings | FileUtil Templates.
 */

@Service
@Transactional
public class RoomServiceImpl extends DaoSupport<Room> implements RoomService {

    @Override
    public List<Room> arrangeRoom(String orderId, int singerNum) {

        Query query = em.createQuery("from Room r where r.size >= ?1 and r.good = true and r not in (select ru.room from RoomUsed ru where ru.startTime <= ?2 and ru.pay =?3)");
        query.setParameter(1, singerNum).setParameter(2, ChangeTime.parseDate(ChangeTime.getCurrentDate())).setParameter(3, PayForOrder.NOT_PAY);

        return query.getResultList();
    }

    @Override
    public List<Integer> showTheRoom() {
        Query query = em.createQuery("from RoomUsed r where  r.pay = 0 ");
        List<RoomUsed> roomUseds = query.getResultList();
        List<Integer> ids = new ArrayList<Integer>();
        for(RoomUsed roomUsed:roomUseds){
            ids.add(roomUsed.getRoom().getRoomId());
        }
        return ids;
    }
}
